package com.base.myproject.client.HR.bcode;

import java.util.Date;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.print.ReportPrintDialog;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseRadioGroup;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Bcode_LeaveInquiry extends BaseJDBCPanel {
	
	private BaseStore<BusiObject> store;
	private BaseFormPanel basicForm;
	private BaseFormPanel leaveSurveyForm;
	private TextField<String> name;
	private TextField<String> position;
	private DateField entrydate;
	private DateField leavedate;
	
	public Bcode_LeaveInquiry(){
		String sql = "select bcode.bcode,bcode.bname,employinnertitle,employstartdate,Resignationdate from bcode,bcode_job where " +
					 " bcode.bcode = bcode_job.bcode and bcode.bcode='"+BcodeMain.store.getValue("bcode")+"'";
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2.getDataSet(sql,new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(DataSet result) {
				init();
				name.setValue(result.getValue(0, "bname"));
				position.setValue(result.getValue(0,"employinnertitle"));
				entrydate.setValue(toDateFromString(result.getValue(0,"employstartdate")));
				leavedate.setValue(toDateFromString(result.getValue(0, "Resignationdate")));
			}});
	}
	
	public void init(){
		Widget();
		registerMainStore(store);
		registerStore(store);
	}
	
	public void complete() {
		super.complete();
		print.addListener(Events.Select, getListener());
		getToolbarll().add(print);
	}
	
	private void Widget(){
		HorizontalPanel hp = new HorizontalPanel();
		hp.setSpacing(5);
		VerticalPanel vp = new VerticalPanel();
		vp.add(getBasicForm());
		vp.add(getLeaveSurveyForm());
		ContentPanel panel = new ContentPanel();
		panel.add(vp);
		panel.setHeaderVisible(false);
		hp.add(panel);
		add(hp);
		this.layout();
	}
	
	public BaseStore<BusiObject> getStore(){
		if(store == null){
			createStore();
		}
		return store;
	}
	
	private BaseStore<BusiObject> createStore(){
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("bname");
		type.getField("bname").setType(String.class);
		type.addField("employinnertitle");
		type.getField("employinnertitle").setType(String.class);
		type.addField("entrydate");
		type.getField("entrydate").setType(Date.class);
		type.addField("leavedate");
		type.getField("leavedate").setType(Date.class);
		type.addField("leavereason");
		type.getField("leavereason").setType(Integer.class);
		type.addField("worktutor");
		type.getField("worktutor").setType(Integer.class);
		type.addField("workevaluate");
		type.getField("workevaluate").setType(Integer.class);
		type.addField("workstate");
		type.getField("workstate").setType(Integer.class);
		type.addField("worketreat");
		type.getField("worketreat").setType(Integer.class);
		type.addField("workrelationwithsupervisours");
		type.getField("workrelationwithsupervisours").setType(Integer.class);
		type.addField("workrelationwithcolleagues");
		type.getField("workrelationwithcolleagues").setType(Integer.class);
		type.addField("hostelsystem");
		type.getField("hostelsystem").setType(Integer.class);
		type.addField("salary");
		type.getField("salary").setType(Integer.class);
		type.addField("returninfuture");
		type.getField("returninfuture").setType(Integer.class);
		type.addField("trainondep");
		type.getField("trainondep").setType(Integer.class);
		type.addField("upgradechance");
		type.getField("upgradechance").setType(Integer.class);
		type.addField("recreationalactivities");
		type.getField("recreationalactivities").setType(Integer.class);
		type.addField("forhruseonly");
		type.getField("forhruseonly").setType(String.class);
		
		StoreInfo info = new StoreInfo();
		info.setTablename("Hr_employResignedSurvey");
		info.setKeyname(new String[]{ "icode" });
		info.setSql("select * from Hr_employResignedSurvey where bcode='"+BcodeMain.store.getValue("bcode")+"'");
//		info.setParent(BcodeMain.store);
//		info.setLinkparentcolumns(new String[] {"bcode"});
//		info.setLinkparent_chile_columns(new String[]{"bcode"});
		store = new BaseStore<BusiObject>(type,info);
		return store;
	}
	
	private BaseFormPanel getBasicForm(){
		basicForm = new BaseFormPanel(getStore(),false);
		TableLayout tl = new TableLayout(4);
		TableData td1 = new TableData();
		td1.setHorizontalAlign(HorizontalAlignment.LEFT);
		td1.setVerticalAlign(VerticalAlignment.MIDDLE);
		td1.setColspan(1);
		td1.setPadding(1);
		basicForm.setLayout(tl);
		basicForm.setHeaderVisible(false);
		basicForm.setBodyBorder(false);
		
		basicForm.add(new Label("姓名　Name:"),td1);
		name = new TextField<String>();
		name.setName("bname");
		basicForm.add(name,td1);
		
		basicForm.add(new Label("职位　Position:"),td1);
		position = new TextField<String>();
		position.setName("employinnertitle");
		basicForm.add(position,td1);
		
		basicForm.add(new Label("入职日期　Date-joined"),td1);
		entrydate = new DateField();
		entrydate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		entrydate.setName("entrydate");
		basicForm.add(entrydate,td1);
		
		basicForm.add(new Label("辞职日期　Date-resigned"),td1);
		leavedate = new DateField();
		leavedate.getPropertyEditor().setFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		leavedate.setName("leavedate");
		basicForm.add(leavedate,td1);
		basicForm.inited();
		return basicForm;
	}
	
	private BaseFormPanel getLeaveSurveyForm(){
		leaveSurveyForm = new BaseFormPanel(getStore(),false);
		FormData formData = new FormData("100%"); 
		FormLayout layout = new FormLayout();  
		leaveSurveyForm.setHeaderVisible(false);
		leaveSurveyForm.setBodyBorder(false);
		layout.setLabelAlign(LabelAlign.TOP); 
		leaveSurveyForm.setLayout(layout);
		
		String titleLeaveReason[] = new String[]{"1、转换工作性质 Change job narue/field","2、个人健康问题 Personal health problem","" +
	    		"3、进修  Further studies","4、工作更佳，薪酬更高 Better job，higher salary",
	    		"5、家庭问题 Family affairs","6、其他 Others"};
	    BaseRadioGroup leaveReasonGroup = new BaseRadioGroup(titleLeaveReason,false);
	    leaveReasonGroup.setName("leavereason");
	    leaveReasonGroup.setFieldLabel("离职原因　Reason　Of　Leaving"); 
	    leaveSurveyForm.add(leaveReasonGroup,formData);
	    
	    String titleworktutor[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup worktutorGroup = new BaseRadioGroup(titleworktutor,true);
	    worktutorGroup.setName("worktutor");
	    worktutorGroup.setFieldLabel("1、您是否由经理或领班处接收到足够的工作指示和教导");  
	    worktutorGroup.setHeight(20);
	    leaveSurveyForm.add(worktutorGroup,formData);
	    
	    String titleworkevaluate[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup workevaluateGroup = new BaseRadioGroup(titleworkevaluate,true);
	    workevaluateGroup.setName("workevaluate");
	    workevaluateGroup.setFieldLabel("2、在您任职期间，经理或主管是否阶段性地对您的工作进行表现评估");  
	    workevaluateGroup.setHeight(20);
	    leaveSurveyForm.add(workevaluateGroup,formData);
	    
	    String titleworkstate[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup workstateGroup = new BaseRadioGroup(titleworkstate,true);
	    workstateGroup.setName("workstate");
	    workstateGroup.setFieldLabel("3、您工作开心吗");  
	    workstateGroup.setHeight(20);
	    leaveSurveyForm.add(workstateGroup,formData);
	    
	    String titleworketreat[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup worketreatGroup = new BaseRadioGroup(titleworketreat,true);
	    worketreatGroup.setName("worketreat");
	    worketreatGroup.setFieldLabel("4、您受到的对待是否公正");  
	    worketreatGroup.setHeight(20);
	    leaveSurveyForm.add(worketreatGroup,formData);
	    
	    String titleworkrelationwithsupervisours[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup workrelationwithsupervisoursGroup = new BaseRadioGroup(titleworkrelationwithsupervisours,true);
	    workrelationwithsupervisoursGroup.setName("workrelationwithsupervisours");
	    workrelationwithsupervisoursGroup.setFieldLabel("5、您和您的上司关系怎样");  
	    workrelationwithsupervisoursGroup.setHeight(20);
	    leaveSurveyForm.add(workrelationwithsupervisoursGroup,formData);
	    
	    String titleworkrelationwithcolleagues[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup workrelationwithcolleaguesGroup = new BaseRadioGroup(titleworkrelationwithcolleagues,true);
	    workrelationwithcolleaguesGroup.setName("workrelationwithcolleagues");
	    workrelationwithcolleaguesGroup.setFieldLabel("6、您和您的同事关系怎样");  
	    workrelationwithcolleaguesGroup.setHeight(20);
	    leaveSurveyForm.add(workrelationwithcolleaguesGroup,formData);
	    
	    String titlehostelsystem[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup hostelsystemGroup = new BaseRadioGroup(titlehostelsystem,true);
	    hostelsystemGroup.setName("hostelsystem");
	    hostelsystemGroup.setFieldLabel("7、您对酒店的规章制度的运作和合理性满意吗");  
	    hostelsystemGroup.setHeight(20);
	    leaveSurveyForm.add(hostelsystemGroup,formData);
	    
	    String titlesalary[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup salaryGroup = new BaseRadioGroup(titlesalary,true);
	    salaryGroup.setName("salary");
	    salaryGroup.setFieldLabel("8、您对工资是否满意");  
	    salaryGroup.setHeight(20);
	    leaveSurveyForm.add(salaryGroup,formData);
	    
	    String titlereturninfuture[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup returninfutureGroup = new BaseRadioGroup(titlereturninfuture,true);
	    returninfutureGroup.setName("returninfuture");
	    returninfutureGroup.setFieldLabel("9、您会考虑将来再回来工作吗");  
	    returninfutureGroup.setHeight(20);
	    leaveSurveyForm.add(returninfutureGroup,formData);
	    
	    String titletrainondep[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup trainondepGroup = new BaseRadioGroup(titletrainondep,true);
	    trainondepGroup.setName("trainondep");
	    trainondepGroup.setFieldLabel("9、在您的部门，培训是经常进行的吗");  
	    trainondepGroup.setHeight(20);
	    leaveSurveyForm.add(trainondepGroup,formData);
	    
	    String titleupgradechance[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup upgradechanceGroup = new BaseRadioGroup(titleupgradechance,true);
	    upgradechanceGroup.setName("upgradechance");
	    upgradechanceGroup.setFieldLabel("10、您是否认为员工在酒店充分享受到成长和学习的机会");  
	    upgradechanceGroup.setHeight(20);
	    leaveSurveyForm.add(upgradechanceGroup,formData);
	    
	    String titlerecreationalactivities[] = new String[]{"1分","2分","3分","4分","5分"};
	    BaseRadioGroup recreationalactivitiesGroup = new BaseRadioGroup(titlerecreationalactivities,true);
	    recreationalactivitiesGroup.setName("recreationalactivities");
	    recreationalactivitiesGroup.setFieldLabel("11、员工的娱乐活动是否丰富多彩");  
	    recreationalactivitiesGroup.setHeight(20);
	    leaveSurveyForm.add(recreationalactivitiesGroup,formData);
	    
	    TextArea txtForhruseonly = new TextArea();
	    txtForhruseonly.setName("forhruseonly");
	    txtForhruseonly.setSize(200, 40);
	    txtForhruseonly.setFieldLabel("以下部分由人力资源部填写");
	    leaveSurveyForm.add(txtForhruseonly,formData);
	    leaveSurveyForm.inited();
		return leaveSurveyForm;
	}
	
	private Date toDateFromString(String date){
		Date datefrom = new Date();
		if(date != null){
			String[] dates = date.split("-");
			int year = Integer.parseInt(dates[0]);
			int month = Integer.parseInt(dates[1]);
			int day = Integer.parseInt(dates[2].substring(0,2));
			datefrom.setYear(year-1900);
			datefrom.setMonth(month-1);
			datefrom.setDate(day);
		}
		return datefrom;
	}

	@Override
	protected void save() {
		String sqlcount = "select count(*) from Hr_employResignedSurvey where bcode='"+BcodeMain.store.getValue("bcode")+"'";
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService2.getDataSet(sqlcount,new AsyncCallback<DataSet>(){
			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(DataSet result) {
				if(result.getRowCount() != 0){
					if(Integer.parseInt(result.getValue(0, 0)) == 0){
						String sql = " insert into Hr_employResignedSurvey (bcode,bname,employinnertitle,entrydate,leavedate) " +
						 " select bcode.bcode,bcode.bname,employinnertitle,employstartdate,lastjobdate from bcode,bcode_job where " +
						 " bcode.bcode = bcode_job.bcode and bcode.bcode='"+BcodeMain.store.getValue("bcode")+"'";
						command(sql);
					}
				}
			}}); 
		super.save();
	}
	
	private boolean  command(String sql){
		CommandSyncsql csql = new CommandSyncsql();//执行sql命令
		String delsql =sql;
		csql.getV().add(delsql);
		if(csql.getV().size()<=0){
			return false;
		}else{
			CommandSyncContainer list = new CommandSyncContainer();//可以添加的命令
			 list.add(csql);
		
			 GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
			 greetingService.SendCommandSync(list,new AsyncCallback<CommandSyncReturnObject>() {
				@Override
				public void onFailure(Throwable caught) {
					MessageBox.alert("错误","网络连接失败", null);
				}
				@Override
				public void onSuccess(CommandSyncReturnObject result) {
					if (result.isB()) {
					//	Info.display("提示信息", "添加成功");								
					}
					else{
						MessageBox.alert("错误","错误，错误代码"+result.getMessage(), null);
									
					}
				}
			});
		}
		return true;
	}	
	
	int sync = 0;                                                      
	Listener<BaseEvent> defaultlistener;   
	String time = "";
                                                                     
	public Listener<BaseEvent> getListener() {                                
		if(defaultlistener!=null)                                        
		{                                                                
			return defaultlistener;                                        
		}                                                                
                                                                     
		defaultlistener = new Listener<BaseEvent>() {                    
			public void handleEvent(BaseEvent be) {                        
				sync++;                                                    
				try {                                                      
					if (sync == 1) {                                   
						if (be.getType() == Events.Select) { 
							if(be.getSource().equals(print)){
								print();
							}
						}                                       
					}                                                  
				} finally {                                                
					sync--;                                            
				}                                                          
			}                                                              
		};                                                               
		return defaultlistener;                                          
	}
	
	public void print(){
		String bcode = BcodeMain.store.getValue("bcode").toString();
		ReportPrintDialog rpd = new ReportPrintDialog();
		rpd.setJspUrl("LeaveInquiry");
		String data = bcode;
		rpd.setData(data);
		rpd.show();
	}
}
